import pandas as pd
from jobs.sql_mehods import *

db = MySQLHelper(host='mmservice-05.mysql.hotgrid.cn', port=3306,
                 user='electricity_api_service', password='GJlfh7&#jg',
                 db='electricity_data', charset='utf8')
def update_dev_rel(self, rel_df, project_id):
    # 1. 获取数据库存储的设备信息
    data_df = pd.DataFrame(self.get_all_dev(project_id))

    # 2. 删除数据库存储的设备关联信息
    self.delete_all_dev_rel(project_id)

    # 3. 重新录入所有的设备关联信息
    rel_df = pd.merge(rel_df, data_df, how='inner', left_on='产污编号', right_on='设备编号')
    rel_df = pd.merge(rel_df, data_df, how='inner', left_on='治污编号', right_on='设备编号')
    for i in rel_df[['RtuId_x', 'DevId_x', 'DevId_y']].to_dict(orient='records'):
        insert_point_sql = f"""
            replace INTO rel_yield_treat(ent_id, yield_dev_id, treat_dev_id, project_id)
            VALUE({int(i['RtuId_x'])},{int(i['DevId_x'])}, {int(i['DevId_y'])}, {project_id});
            """
        # with self.conn.cursor() as cur:
        data=db.execute_select(insert_point_sql)
        print(data)
        print('rel_yield_treat更新设备关联：', i)